package cn.springmvc.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.springmvc.dao.BaseDAO;
import cn.springmvc.model.ResultBaseBean;
import cn.springmvc.model.entity.UserBean;
import cn.springmvc.service.IUserService;
import cn.springmvc.util.SessionManager;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	@Qualifier("userDAOImpl")
	private BaseDAO<UserBean> userDAO;
	
	ResultBaseBean<UserBean> resultBaseBean;


	public ResultBaseBean<UserBean> insert(UserBean t) {
		resultBaseBean = new ResultBaseBean<UserBean>();
		
		UserBean queryUserBean = new UserBean();
		queryUserBean.setPhoneNumber(t.getPhoneNumber());
		List<UserBean> userBeanList = userDAO.select(queryUserBean);
		if(userBeanList != null && userBeanList.size()>0){
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("该手机号已经被注册");
		}else{	
			boolean result = userDAO.insert(t);
			
			if(result){
				resultBaseBean.setSuccess(true);
			}else{
				resultBaseBean.setSuccess(false);
				resultBaseBean.setMessage("添加用户失败");
			}
		}
		return resultBaseBean;
	}

	public ResultBaseBean<UserBean> select(UserBean t) {
		resultBaseBean = new ResultBaseBean<UserBean>();
		List<UserBean> userBeanList = userDAO.select(t);
		if(userBeanList != null){
			resultBaseBean.setResultList(userBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取用户列表失败");
		}
		
		return resultBaseBean;
	}

	public ResultBaseBean<UserBean> delete(UserBean t) {
		resultBaseBean = new ResultBaseBean<UserBean>();
		boolean result = userDAO.delete(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("删除用户失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<UserBean> update(UserBean t) {
		resultBaseBean = new ResultBaseBean<UserBean>();
		boolean result = userDAO.update(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("更新用户失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<UserBean> selectOne(UserBean t) {
		resultBaseBean = new ResultBaseBean<UserBean>();
		UserBean userBean = userDAO.selectOne(t);
		if(userBean != null){
			List<UserBean> userBeanList = new ArrayList<UserBean>();
			userBeanList.add(userBean);
			resultBaseBean.setResultList(userBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取用户失败");
		}
		
		return resultBaseBean;
	}

	public ResultBaseBean<UserBean> login(HttpSession httpSession, UserBean t){
		resultBaseBean = new ResultBaseBean<UserBean>();
		List<UserBean> userBeanList = userDAO.select(t);
		
		if(userBeanList != null){
			resultBaseBean.setResultList(userBeanList);
			resultBaseBean.setSuccess(true);
			
			if(userBeanList != null && userBeanList.size() > 0){
				UserBean userBean = userBeanList.get(0);
				httpSession.setAttribute("userBean", userBean);
			}
			
			String sessionId = httpSession.getId();
			SessionManager sessionManager = SessionManager.getInstance();
			sessionManager.addSession(sessionId, httpSession);
			
			resultBaseBean.setSessionId(sessionId);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("登录失败");
		}
			
		return resultBaseBean;
	}
	
	public ResultBaseBean<UserBean> logout(String sessionId){
		resultBaseBean = new ResultBaseBean<UserBean>();
		
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			sessionManager.removeSessionById(sessionId);
		}
		resultBaseBean.setSuccess(true);	
		return resultBaseBean;
	}
	
	public ResultBaseBean<UserBean> getResultBaseBean() {
		return resultBaseBean;
	}

	public void setResultBaseBean(ResultBaseBean<UserBean> resultBaseBean) {
		this.resultBaseBean = resultBaseBean;
	}

}
